﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XnaGameStudio1
{
    //-----------------------------------------------------------------------------------------------
    class XnaGraphicsDevice : IGraphicsDeviceService, IGraphicsDeviceManager
    {
        static private GraphicsDevice graphicsDevice;
        private Control renderControl;

        //-------------------------------------------------------------------------------------------
        public XnaGraphicsDevice(Control control)
        {
            renderControl = control;
        }

        //-------------------------------------------------------------------------------------------
        static public GraphicsDevice Device
        {
            get { return graphicsDevice; }
        }

        //-------------------------------------------------------------------------------------------
        #region IGraphicsDeviceService Members
        //----------------------------------------------------------------------------------------------------

            public event EventHandler<EventArgs> DeviceCreated;
            public event EventHandler<EventArgs> DeviceDisposing;
            public event EventHandler<EventArgs> DeviceReset;
            public event EventHandler<EventArgs> DeviceResetting;

            //---------------------------------------------------------------------------------------
            public GraphicsDevice GraphicsDevice
            {
                get { return graphicsDevice; }
            }

        #endregion
        //----------------------------------------------------------------------------------------------------


        //-------------------------------------------------------------------------------------------
        #region IGraphicsDeviceManager Members
        //----------------------------------------------------------------------------------------------------

            //---------------------------------------------------------------------------------------
            public bool BeginDraw()
            {
                graphicsDevice.Clear(Color.CornflowerBlue);
                return true;
            }

            //---------------------------------------------------------------------------------------
            public void Clear(Color color)
            {
                graphicsDevice.Clear(color);
            }

            //---------------------------------------------------------------------------------------
            public void CreateDevice()
            {
                PresentationParameters pp = new PresentationParameters();
                pp.IsFullScreen = false;
                pp.BackBufferHeight = renderControl.Height;
                pp.BackBufferWidth = renderControl.Width;
                pp.DeviceWindowHandle = renderControl.Handle;

                graphicsDevice = new GraphicsDevice(
                        GraphicsAdapter.Adapters[0], GraphicsProfile.Reach, pp 
                        );
                
                GraphicsArgs e = new GraphicsArgs( graphicsDevice );

                if ( DeviceCreated != null )
                    DeviceCreated( this, e );
            }
            //---------------------------------------------------------------------------------------
            public void EndDraw()
            {
                graphicsDevice.Present();
            }

        #endregion
        //----------------------------------------------------------------------------------------------------
    }

    public class GraphicsArgs : EventArgs
    {
        public GraphicsArgs(GraphicsDevice device)
        {
            graphicsDevice = device;
        }

        private GraphicsDevice graphicsDevice;

        public GraphicsDevice GraphicsDevice
        {
            get { return graphicsDevice; }
            set { graphicsDevice = value; }
        }
    }
}
